<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Andrey
 * Date: 25.01.12
 * Time: 20:40
 * To change this template use File | Settings | File Templates.
 */
require_once('inc/app.php');

if($_POST) {

    $form = $_POST;

    // Save the company
    $result = $dbConnect->query(
        "INSERT INTO company (name, email, short_desc, phone, location_id)
        VALUES('{$form['name']}', '{$form['email']}', '{$form['short_desc']}', '{$form['phone']}', ".(int)$form['location'].");"
    );
    $companyLastId = $dbConnect->insert_id;

    // Add category
    foreach($form['cat'] as $key => $value) {
        $dbConnect->query(
            "INSERT INTO company_has_category (company_id, category_id)
            VALUES({$companyLastId}, ". (int)$value .");"
        );
    }

    // Add a rating
    $result = $dbConnect->query(
        "INSERT INTO company_rating (company_id, rating)
        VALUES({$companyLastId}, ".(int)$form['rating'].");"
    );

    header("Location: list.php", true);
    exit();

} else {

    // Load category
    $categoryCollection = getRowSet($dbConnect->query(
            "SELECT * FROM category"
        )
    );

    // Load location
    $locationCollection = getRowSet($dbConnect->query(
            "SELECT * FROM location"
        )
    );

    ob_end_flush();
    include 'template/add.php';
}

?>